package com.example.hans.internetandroid;

import android.os.Bundle;
import android.support.annotation.LongDef;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpTest extends AppCompatActivity implements View.OnClickListener{


    private Button mButton;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.okhttp_layout);

        mButton = (Button)findViewById(R.id.btn_getData);
        mTextView = (TextView)findViewById(R.id.tv_content);


        mButton.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_getData: {
                sendRequestWithOkHttp();
            }
            default:
        }


    }

    private void sendRequestWithOkHttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient;
                Request request;
                Response response;
                String responseData;

                try {

                    okHttpClient = new OkHttpClient();
                    request = new Request.Builder()
                            .url("http://192.168.5.2/android/getData.json")
                            .build();
                    response = okHttpClient.newCall(request).execute();
                    responseData = response.body().string();
                    showResponse(responseData);

                    parseJSONWithJSONObject(responseData);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    private void showResponse(final String response) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mTextView.setText(response);
            }
        });
    }

    static final String TAG = "OkHttpTest";

    //利用JSONObject  解析json数据
    private void parseJSONWithJSONObject(String jsonData) {
        try {
            JSONArray jsonArray = new JSONArray(jsonData);
            String id = "";
            String name = "";
            String version = "";

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                id = jsonObject.getString("id");
                name = jsonObject.getString("name");
                version = jsonObject.getString("version");

                Log.d(TAG,"id is " + id);
                Log.d(TAG,"name is " + name);
                Log.d(TAG,"version is " + version);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //利用PuLL解析xml 数据
    private void parseXMLWithPull(String xmlData) {
        try {

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            xmlPullParser.setInput(new StringReader(xmlData));

            int eventType = xmlPullParser.getEventType();
            String id = "";
            String name = "";
            String version = "";

            while (eventType != XmlPullParser.END_DOCUMENT) {
                String nodeName = xmlPullParser.getName();

                switch (eventType) {
                    case XmlPullParser.START_TAG: {
//                        Log.d(TAG+"100",nodeName);
                        if ("id".equals(nodeName)) {
                            id = xmlPullParser.nextText();
                        } else if ("name".equals(nodeName)) {
                            name = xmlPullParser.nextText();
                        } else if ("version".equals(nodeName)) {
                            version = xmlPullParser.nextText();
                        }
                        break;
                    }
                    case XmlPullParser.END_TAG: {
                        if ("app".equals(nodeName)) {
                            Log.d(TAG,"id is " + id);
                            Log.d(TAG,"name is " + name);
                            Log.d(TAG,"version is " + version);
                        }
                        break;
                    }
                    default:

                }
                eventType = xmlPullParser.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
